require('Shaders')
require('Sound')

Player = {}
Player.__index = Player

function Player.load(world)

	local player = {}
	setmetatable(player, Player)
	player.x = 600
	player.y = 400
	player.maxRadius = 30
	player.minRadius = 20
	player.radius = 25
	player.color = {1,1,1,1}
	player.health = 5000
	player.isPulseActive = false
	player.currentPulseCD = 0
	player.maxPulseCD = 360
	player.pulseTimer = 120
	player.breathDirection = "in"
	player.worldMaxY = world.height
	player.worldMaxX = world.width
	player.pixelEffect = love.graphics.newPixelEffect((Shaders.getPlayerEffect()):format(#player))
	player.pixelEffect:send('position', {player.x, player.y})
	player.pixelEffect:send('size', player.radius)
	player.pixelEffect:send('col', player.color)
	player.particleSys = Particles.getFuzzParticleSys(0, 0)
	player.particleSys:start()
     
     font_end = love.graphics.newFont("Fonts/Orbitron Black.ttf", 48)
     
	return player
	
end


function Player:draw()

	love.graphics.draw(self.particleSys, self.x, self.y)
	love.graphics.setPixelEffect(self.pixelEffect)
	love.graphics.setColorMode("modulate")
	love.graphics.setBlendMode("additive")
	
end

function Player:update(dt)

	self.health = self.health - 2
     
     if self.health < 1 then
		--invoke loss condition
          love.graphics.setFont(font_end)
          gameOver = true
     end
	--Calculate color
	colorPerc = 1- self.currentPulseCD/self.maxPulseCD
	
	if self.currentPulseCD == 0 then
		color = {1, 1, 1, 1}
	else
		color = {1, .5 + colorPerc/2, 0 + colorPerc, 1}--.5 + math.pow(colorPerc,2)/2, 1}
	end
	
	self.color = color
	
	
	--Calculate Pulse CD
	if self.currentPulseCD ~= 0 then
		self.currentPulseCD = self.currentPulseCD - 1
	end
	
	--Calculate radius
	if self.breathDirection == "in" then
	
		if self.radius < self.minRadius then
               Sound:beat()
			self.breathDirection = "out"
			
		end
		
		if self.health > 0 then
			self.radius = self.radius - 750*(1.0/self.health)
		else
			gameOver = true
		end
		
	end
	
	if self.breathDirection == "out" then
	
		if self.radius > self.maxRadius then
               Sound:beat()
			self.breathDirection = "in"
			
		end
		
		if self.health > 0 then
			self.radius = self.radius + 750*(1.0/self.health)
		else
			gameOver = true
		end
			
	end
	
	self.pixelEffect:send('position', {self.x, self.y})
	self.pixelEffect:send('size', self.radius)
	self.pixelEffect:send('col', self.color)
	self.particleSys:update(dt, self.glowhp)
	
end

function Player:getX()

	return self.x
	
end

function Player:getY()

	return self.y
	
end

function Player:getHealth()

	return self.health
	
end

function Player:setX(newX)

	--Bounds check
	if (newX > self.maxRadius) and (newX < (self.worldMaxX - self.maxRadius)) then
	
		self.x = newX
		
		return true
		
	else
	
		return false
		
	end
	
end

function Player:setY(newY)

	--Bounds check
	if (newY > self.maxRadius) and (newY < (self.worldMaxY - self.maxRadius)) then
	
		self.y = newY
		
		return true
		
	else
	
		return false
		
	end
	
end